var member_selected_id;
var id_job_title_edit = -1;
var flag = new Array();
flag['mb_fullname'] = 0;
flag['mb_fullname'] = 0;
flag['mb_fullname'] = 0;
flag['mb_fullname'] = 0;


function deleteMember(member_id){
	var answer = confirm("Are you sure you want to delete this member?");
	if (answer)
	{
		callAJAX('xoad_handler', 'deleteMember', $H({id:'message_list',
				member_id: member_id}),
				deleteMember_callback);
	}
}

var deleteMember_callback = function deleteMember_callback(result) {
	$('message_list').innerHTML = result;
}

function loadAJAX()
{
	eval(startup_functions);
//	displaySearchMemberForm();
	$('toolbox').style.display = "";

}

function memberList(){
	displayPannels ('memberList');
	getMemberList();
}

function memberCreate(){
	displayPannels ('memberCreate');
	displayCreateMemberForm();
}

function memberSearch(){
	displaySearchMemberForm();
	if ($('memberSearch').style.display ==  'none')
	
		$('memberSearch').style.display =  '';
	else
		$('memberSearch').style.display =  'none';
}

function getMemberList(){
	callAJAX('xoad_handler', 'getMemberList', $H({
				id: 'memberList'}),
				getMemberList_callback);
}

var getMemberList_callback = function getMemberList_callback(result) {
	$('message_list').innerHTML = result;
}

function displayCreateMemberForm(){
	displayPannels('memberCreate');
	callAJAX('xoad_handler', 'displayCreateMemberForm', $H({
				id: 'memberCreate'}),
				displayCreateMemberForm_callback);
}

var displayCreateMemberForm_callback = function displayCreateMemberForm_callback(result) {
	$('message').innerHTML = result;
}

function displaySearchMemberForm(){
	callAJAX('xoad_handler', 'displaySearchMemberForm', $H({
				id: 'memberSearch'}),
				displaySearchMemberForm_callback);
}

var displaySearchMemberForm_callback = function displaySearchMemberForm_callback(result) {
	
}

function loadJobTitle(department, jobtitle){
	var department_id_selected = $(department).value;
	callAJAX('xoad_handler', 'loadJobTitle', $H({
				id: jobtitle, department_id: department_id_selected}),
				loadJobTitle_callback);
}
var loadJobTitle_callback = function loadJobTitle_callback(result) {
	if (id_job_title_edit!=-1)
	{
		$('jobtitle_edit').value = id_job_title_edit;
	}
}

function checkInputData(){
	var message 			= "";
	var email 				= $('email').value;
	var password 			= $('password').value;
	var password_confirm 	= $('password_confirm').value;
	var department 			= $('department').value;
	var jobtitle 			= $('job_title').value;
	var fullname 			= $('fullname').value;
	var usertype 			= $('member_type').value;
	if (email == "" || email == null){
		message = msg_email_invalid;
	}
	else if (password == "" || password==null){
		message = msg_password_invalid;
	}
	else if (password_confirm != password){
		message = msg_password_invalid;
	}
	else if (department == 0){
		message = msg_department_invalid;
	}
	else if (jobtitle == 0){
		message = msg_jobtitle_invalid;
	}
	else if (fullname == "" || fullname==null){
		message = msg_fullname_invalid;
	}
	else if (usertype == 0){
		message = msg_usertype_invalid;
	}
	
	return message;
}

function insertAMember(){
	message = checkInputData();
	if (message != "") //input invalid
	{
		$('message').innerHTML = message;
	}
	else //input valid
	{
		var form = xoad.html.exportForm('member_create_form');
		callAJAX('xoad_handler', 'insertAMember', $H({
				id: 'message', form: form}),
				insertAMember_callback);
	}
}
var insertAMember_callback = function insertAMember_callback(result) {
	$('message').innerHTML = result;
	getMemberList();
}

function searchMember(){
	$('memberList').style.display = '';
	$('memberCreate').style.display = 'none';
	
	$('member_search_form').page.value = 1;	
	var form = xoad.html.exportForm('member_search_form');
	callAJAX('xoad_handler', 'searchMember', $H({
			id: 'memberList', form: form}),
			searchMember_callback);
}
var searchMember_callback = function searchMember_callback(result) {
	$('message_list').innerHTML = result;
}

function onPage(intPage,attach)
{
	form.page = intPage;
	callAJAX('xoad_handler', 'searchMember', $H({
		id: 'memberList', form: form}), searchMember_callBack);
}

function getMemberDetail(member_id){

	displayPannels ('memberDetail');	
	callAJAX('xoad_handler', 'getMemberDetail', $H({
				id: 'memberDetail', member_id: member_id}),
				getMemberDetail_callback);
}

var getMemberDetail_callback = function getMemberDetail_callback(result) {
	//$('message').innerHTML = result;
}
function displayPannels (pannel)
{
	var arrayPannels = new Array('memberCreate','memberList','memberDetail','memberEdit');
	
	for (var i = 0; i < arrayPannels.length; i++)
	if(arrayPannels[i]==pannel)
		$(arrayPannels[i]).style.display = "";
	else
		$(arrayPannels[i]).style.display = "none";		

}

function displayEditForm(member_id){

	displayPannels ('memberEdit');
	member_selected_id = member_id;
	callAJAX('xoad_handler', 'displayEditForm', $H({
				id: 'memberEdit', member_id: member_id}),
				displayEditForm_callback);
}

var displayEditForm_callback = function displayEditForm_callback(result) {
	id_job_title_edit = result['jobtitle_edit'];
	xoad.html.importForm('member_update_form',result);
	loadJobTitle('department_edit','jobtitle_edit');	
}

function updateMember(){
	var form = xoad.html.exportForm('member_update_form');
	callAJAX('xoad_handler', 'UpdateMember', $H({
				id: 'message_update', member_id: member_selected_id, form: form}),
				updateMember_callback);
}

var updateMember_callback = function updateMember_callback(result) {
	$('message_update').innerHTML = result;
}


//column: sort_by column
function sortMember(column){
	var form = xoad.html.exportForm('member_search_form');
	
	if (flag['mb_fullname'] == 0) {	//sort ascending	
		callAJAX('xoad_handler', 'sortMember', $H({
				id: 'memberList', form: form,
				order_by: column, order_type: "asc"}), sortMember_callBack);
		flag['mb_fullname'] = 1;
	}
	else //sort descending
	{ 
		callAJAX('xoad_handler', 'sortMember', $H({
				id: 'memberList', form: form,
				order_by: column, order_type: "desc"}), sortMember_callBack);
		flag['mb_fullname'] = 0;	
	}

}

var sortMember_callBack = function sortMember_callBack(results){}


Event.observe(window, 'load', loadAJAX);